东风启辰小程序端
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.

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