东风启辰小程序端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

486 rindas
15KB

  1. // pages/userMsg/userMsg.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. },
  9. /**
  10. * 组件的初始数据
  11. */
  12. data: {
  13. imgUrl: app.globalData.urlStatic,//图片路径
  14. submitData: {
  15. realName: "",//姓名
  16. mobile: "",//电话
  17. province: "",//经销商省份
  18. city: "",//经销商城市
  19. agent_code:"",//经销商编码
  20. agentDetail:"",//经销商详情
  21. addressDetail: "",//地址详情
  22. captcha:"",
  23. scene:"",//场景值名称
  24. },
  25. mobile2:"",
  26. selectType:2,
  27. avatarUrlShow:"",
  28. userData:{
  29. avatarUrl:"",
  30. nickName:""
  31. },
  32. isAddress: false,//是否有地址
  33. marginT: 0,
  34. submitDataState:false,
  35. verificationCode:"获取验证码",
  36. sendCode:true,
  37. phoneInputShow: false,//是否显示电话输入框
  38. provinceDataAll: null,//地区所有数据
  39. provinceDataArr: [[""], [""]],//省市数据
  40. provinceDataValue: [0, 0],//选中的省市下标
  41. nowProvince: "",//选中的省市文字
  42. storeArr: [],//专营店数据
  43. storeValue: 0,//选中的专营店下标
  44. },
  45. attached:function(){
  46. if (app.globalData.userPhoneType == "ios") {
  47. this.setData({
  48. marginT: -20
  49. })
  50. }
  51. if (app.globalData.openid) {
  52. this.loadFun();
  53. } else {
  54. app.globalData.openidSuccessFuc = this.loadFun;
  55. }
  56. },
  57. /**
  58. * 组件的方法列表
  59. */
  60. methods: {
  61. loadFun: function () {
  62. if (app.globalData.userMobile) {
  63. this.data.submitData.mobile = app.globalData.userMobile;
  64. this.setData({
  65. phoneInputShow: true,
  66. submitData: this.data.submitData
  67. })
  68. }
  69. if (app.globalData.userInfoData.nickName) {
  70. this.data.userData.avatarUrl = app.globalData.userInfoData.avatarUrl;
  71. this.data.userData.nickName = app.globalData.userInfoData.nickName;
  72. this.setData({
  73. userData: this.data.userData,
  74. avatarUrlShow: app.globalData.userInfoData.avatarUrl
  75. })
  76. }
  77. this.getAddress();
  78. },
  79. getAddress: function () {//获取地址
  80. app.wxRequest(app.globalData.urlRoot + "address/getAddressV2", {}, res => {
  81. console.log(res);
  82. if (res.code == 200) {
  83. if (res.data) {
  84. this.data.isAddress = true;
  85. this.data.submitData.realName = res.data.realName;
  86. this.data.submitData.mobile = res.data.mobile;
  87. this.data.submitData.province = res.data.province;
  88. this.data.submitData.city = res.data.city;
  89. this.data.submitData.agentDetail = res.data.agentDetail;
  90. this.data.submitData.agent_code = res.data.agent_code;
  91. this.data.submitData.addressDetail = res.data.addressDetail;
  92. this.setData({
  93. submitData: this.data.submitData,
  94. mobile2:res.data.mobile
  95. })
  96. // if(!res.data.agentDetail){
  97. this.getDistributorList("","");
  98. // this.getUserLocation();//获取用户当前位置
  99. // }
  100. }else{
  101. this.getUserLocation();//获取用户当前位置
  102. }
  103. } else {
  104. wx.showToast({
  105. title: res.msg,
  106. icon: "none"
  107. })
  108. }
  109. }, this);
  110. },
  111. addAddress: function () {//添加地址
  112. var mobile = this.data.submitData.mobile;
  113. if(this.data.selectType==2){
  114. this.data.submitData.mobile = this.data.mobile2;
  115. }
  116. app.wxRequest(app.globalData.urlRoot + "address/addAddressV2", this.data.submitData, res => {
  117. wx.showToast({
  118. title: res.msg,
  119. })
  120. if (res.code == 200) {
  121. if (app.globalData.skipType == 'mycenter') {
  122. app.globalData.skipType = null;
  123. wx.reLaunch({
  124. url: '/pages/myCenter/myCenter',
  125. })
  126. } else {
  127. this.closeXieyi();
  128. }
  129. }
  130. }, this, "POST")
  131. },
  132. updateAddress: function () {//更新地址
  133. var mobile = this.data.submitData.mobile;
  134. if(this.data.selectType==2){
  135. this.data.submitData.mobile = this.data.mobile2;
  136. }
  137. app.wxRequest(app.globalData.urlRoot + "address/updateAddressV2", this.data.submitData, res => {
  138. wx.showToast({
  139. title: res.msg,
  140. })
  141. if (res.code == 200) {
  142. if (app.globalData.skipType == 'mycenter') {
  143. app.globalData.skipType = null;
  144. wx.reLaunch({
  145. url: '/pages/myCenter/myCenter',
  146. })
  147. } else {
  148. this.closeXieyi();
  149. }
  150. }
  151. }, this, "POST");
  152. },
  153. getNickName: function(e) {//获取昵称
  154. this.data.userData.nickName = e.detail.value;
  155. this.setData({
  156. userData: this.data.userData
  157. })
  158. },
  159. getRealName: function (e) {//获取收货人
  160. this.data.submitData.realName = e.detail.value;
  161. this.setData({
  162. submitData: this.data.submitData
  163. })
  164. },
  165. getMobile: function (e) {//获取手机号码
  166. this.data.submitData.mobile = e.detail.value;
  167. this.setData({
  168. submitData: this.data.submitData
  169. })
  170. },
  171. getMobile2: function (e) {//获取用户输入的电话---无验证码
  172. this.setData({
  173. mobile2:e.detail.value
  174. })
  175. },
  176. getAddressDetail: function (e) {//获取详细地址
  177. this.data.submitData.addressDetail = e.detail.value;
  178. this.setData({
  179. submitData: this.data.submitData
  180. })
  181. },
  182. saveUserMsg:function(){
  183. if (!this.data.submitData.realName) {
  184. wx.showToast({
  185. title: '请输入姓名',
  186. icon: "none"
  187. })
  188. return;
  189. }
  190. if (!this.data.mobile2 && this.data.selectType==2) {
  191. wx.showToast({
  192. title: '请输入电话',
  193. icon: "none"
  194. })
  195. return;
  196. }
  197. if (this.data.selectType == 2) {
  198. if (!this.data.submitData.captcha) {
  199. wx.showToast({
  200. title: '请输入验证码',
  201. icon: "none"
  202. })
  203. return;
  204. }
  205. }
  206. if (!this.data.submitData.addressDetail) {
  207. wx.showToast({
  208. title: '请输入详细地址',
  209. icon: "none"
  210. })
  211. return;
  212. }
  213. this.data.submitData.province = this.data.provinceDataArr[0][this.data.provinceDataValue[0]];
  214. this.data.submitData.city = this.data.provinceDataArr[1][this.data.provinceDataValue[1]];
  215. this.data.submitData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  216. this.data.submitData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  217. this.data.submitData.scene = app.globalData.sceneSource;
  218. // console.log(this.data.submitData);
  219. // return;
  220. wx.showLoading({
  221. title: '保存中',
  222. mask: true
  223. })
  224. if(this.data.submitDataState){
  225. return;
  226. }
  227. this.data.submitDataState = true;
  228. app.wxRequest(app.globalData.urlRoot + "userInfo/updateUserInfo", this.data.userData, res => {
  229. this.data.submitDataState = false;
  230. wx.hideLoading();
  231. if (res.code == 200) {
  232. if(this.data.userData){
  233. var userInfoData = wx.getStorageSync("userInfoData");
  234. if(this.data.userData.avatarUrl){
  235. console.log("userData")
  236. app.globalData.userInfoData.avatarUrl = this.data.userData.avatarUrl;
  237. if(userInfoData){
  238. userInfoData.avatarUrl = this.data.userData.avatarUrl;
  239. }
  240. }
  241. if(this.data.userData.nickName){
  242. app.globalData.userInfoData.nickName = this.data.userData.nickName;
  243. if(userInfoData){
  244. userInfoData.nickName = this.data.userData.nickName;
  245. }
  246. }
  247. wx.setStorageSync('userInfoData', userInfoData)
  248. }
  249. this.submitAddress();
  250. }
  251. }, this, "POST")
  252. },
  253. submitAddress: function () {//保存地址
  254. if (this.data.isAddress) {
  255. this.updateAddress();
  256. } else {
  257. this.addAddress();
  258. }
  259. },
  260. closeXieyi: function () {
  261. this.triggerEvent('address')
  262. },
  263. changeHeadImg:function(){
  264. wx.chooseImage({
  265. count:1,
  266. success:(res)=>{
  267. this.setData({
  268. avatarUrlShow: res.tempFilePaths[0]
  269. })
  270. wx.uploadFile({
  271. url: app.globalData.urlRoot + "upload/uploadImage",
  272. header: {
  273. "OPENID": app.globalData.openid,
  274. "VERSION": 100
  275. },
  276. filePath: res.tempFilePaths[0],
  277. name: "file",
  278. formData:{
  279. filePath:"avatar"
  280. },
  281. success: option => {
  282. var json = JSON.parse(option.data);
  283. if (json.code==200){
  284. this.data.userData.avatarUrl = json.data[0].url;
  285. this.setData({
  286. userData: this.data.userData
  287. })
  288. }else{
  289. wx.showToast({
  290. title: option.msg,
  291. icon: 'none'
  292. })
  293. }
  294. }
  295. })
  296. }
  297. })
  298. },
  299. changeSelectType:function(e){
  300. if(e){
  301. this.setData({
  302. selectType:e.currentTarget.dataset.type
  303. })
  304. }else{
  305. this.setData({
  306. selectType:1
  307. })
  308. }
  309. },
  310. getCaptcha: function (e) {//获取用户输入的验证码
  311. this.data.submitData.captcha = e.detail.value;
  312. this.setData({
  313. submitData:this.data.submitData
  314. })
  315. },
  316. getCode: function (e) {//获取验证码
  317. if (!app.mobileVerify(this.data.mobile2)) {
  318. if (this.data.mobile2) {
  319. wx.showToast({
  320. title: '请输入正确的电话',
  321. icon: 'none'
  322. })
  323. } else {
  324. wx.showToast({
  325. title: '请输入电话',
  326. icon: 'none'
  327. })
  328. }
  329. return;
  330. }
  331. if (!this.data.sendCode) {
  332. return;
  333. }
  334. this.data.sendCode = false;
  335. app.wxRequest(app.globalData.urlRoot + "captcha/sendCaptcha", { mobile: this.data.mobile2 }, res => {
  336. if (res.code == 200) {
  337. this.countDown();
  338. wx.showToast({
  339. title: '验证码获取成功',
  340. icon: "none"
  341. })
  342. this.setData({
  343. verificationCode: 60
  344. })
  345. } else {
  346. this.data.sendCode = true;
  347. wx.showToast({
  348. title: res.msg,
  349. icon: "none"
  350. })
  351. }
  352. }, this)
  353. },
  354. countDown: function () {//倒计时
  355. setTimeout(() => {
  356. this.setData({
  357. verificationCode: this.data.verificationCode - 1
  358. })
  359. if (this.data.verificationCode > 0) {
  360. this.countDown();
  361. } else {
  362. this.setData({
  363. verificationCode: "获取验证码"
  364. })
  365. this.data.sendCode = true;
  366. }
  367. }, 1000);
  368. },
  369. getUserPhone: function (e) {//获取用户手机号
  370. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  371. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  372. if (res.code == 200) {
  373. this.setData({
  374. phoneInputShow: true
  375. })
  376. this.changeSelectType();
  377. if (res.data && res.data.decodeData) {
  378. this.data.submitData.mobile = res.data.decodeData.phoneNumber;
  379. this.setData({
  380. submitData: this.data.submitData
  381. })
  382. }
  383. } else {
  384. wx.showToast({
  385. title: res.msg,
  386. icon: "none"
  387. })
  388. }
  389. }, this);
  390. }
  391. },
  392. getUserLocation: function (e) {
  393. wx.getLocation({
  394. type: 'wgs84', //wgs84 gcj02
  395. success: (res) => {
  396. this.getDistributorList(res.longitude, res.latitude);
  397. },
  398. fail: (res) => {
  399. this.getDistributorList("", "");
  400. }
  401. })
  402. },
  403. getDistributorList: function (longitude, latitude) {//获取经销商列表
  404. app.wxRequest(app.globalData.urlRoot + "agent/getAgentList", { longitude: longitude, latitude: latitude }, res => {
  405. if (res.code == 200) {
  406. //整理数据
  407. var datas = res.data;
  408. var province = [];
  409. var city = [];
  410. for (let i = 0; i < res.data.list.length; i++) {
  411. province.push(res.data.list[i].province);
  412. }
  413. if(this.data.submitData.province){
  414. for(let i=0;i<province.length;i++){
  415. if(province[i]==this.data.submitData.province){
  416. res.data.nearData.provinceIndex = i;
  417. }
  418. }
  419. }
  420. for (let j = 0; j < res.data.list[res.data.nearData.provinceIndex].children.length; j++) {
  421. city.push(res.data.list[res.data.nearData.provinceIndex].children[j].city)
  422. }
  423. if(this.data.submitData.city){
  424. for(let i=0;i<city.length;i++){
  425. if(city[i]==this.data.submitData.city){
  426. res.data.nearData.cityIndex = i;
  427. }
  428. }
  429. }
  430. if(this.data.submitData.agentDetail){
  431. for(let i=0;i<res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children.length;i++){
  432. if(res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children[i]==this.data.submitData.agentDetail){
  433. res.data.nearData.agentIndex = i;
  434. }
  435. }
  436. }else{
  437. res.data.nearData.agentIndex = 0;
  438. }
  439. //将数据赋值给变量
  440. this.setData({
  441. provinceDataAll: res.data.list,
  442. provinceDataArr: [province, city],
  443. provinceDataValue: [res.data.nearData.provinceIndex, res.data.nearData.cityIndex],
  444. nowProvince: province[res.data.nearData.provinceIndex] + " " + city[res.data.nearData.cityIndex],
  445. storeArr: res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children,
  446. storeValue: res.data.nearData.agentIndex
  447. })
  448. } else {
  449. wx.showToast({
  450. title: res.msg,
  451. icon: "none"
  452. })
  453. }
  454. }, this);
  455. },
  456. provinceDataChange: function (e) {
  457. if (e.detail.column == 0) {
  458. var city = [];
  459. for (let i = 0; i < this.data.provinceDataAll[e.detail.value].children.length; i++) {
  460. city.push(this.data.provinceDataAll[e.detail.value].children[i].city);
  461. }
  462. this.data.provinceDataArr[1] = city;
  463. this.setData({
  464. provinceDataArr: this.data.provinceDataArr
  465. })
  466. }
  467. },
  468. provinceDataChane: function (e) {
  469. this.setData({
  470. provinceDataValue: e.detail.value,
  471. nowProvince: this.data.provinceDataArr[0][e.detail.value[0]] + " " + this.data.provinceDataArr[1][e.detail.value[1]],
  472. storeArr: this.data.provinceDataAll[e.detail.value[0]].children[e.detail.value[1]].children,
  473. storeValue: 0
  474. })
  475. },
  476. storeChane: function (e) {
  477. this.setData({
  478. storeValue: e.detail.value
  479. })
  480. }
  481. }
  482. })