东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 line
16KB

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