东风启辰小程序端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

477 lines
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. app.globalData.userInfoData.avatarUrl = this.data.userData.avatarUrl;
  236. userInfoData.avatarUrl = this.data.userData.avatarUrl;
  237. }
  238. if(this.data.userData.nickName){
  239. app.globalData.userInfoData.nickName = this.data.userData.nickName;
  240. userInfoData.nickName = this.data.userData.nickName;
  241. }
  242. wx.setStorageSync('userInfoData', userInfoData)
  243. }
  244. this.submitAddress();
  245. }
  246. }, this, "POST")
  247. },
  248. submitAddress: function () {//保存地址
  249. if (this.data.isAddress) {
  250. this.updateAddress();
  251. } else {
  252. this.addAddress();
  253. }
  254. },
  255. closeXieyi: function () {
  256. this.triggerEvent('address')
  257. },
  258. changeHeadImg:function(){
  259. wx.chooseImage({
  260. count:1,
  261. success:(res)=>{
  262. this.setData({
  263. avatarUrlShow: res.tempFilePaths[0]
  264. })
  265. wx.uploadFile({
  266. url: app.globalData.urlRoot + "upload/uploadImage",
  267. header: {
  268. "OPENID": app.globalData.openid,
  269. "VERSION": 100
  270. },
  271. filePath: res.tempFilePaths[0],
  272. name: "file",
  273. formData:{
  274. filePath:"avatar"
  275. },
  276. success: option => {
  277. var json = JSON.parse(option.data);
  278. if (json.code==200){
  279. this.data.userData.avatarUrl = json.data[0].url;
  280. this.setData({
  281. userData: this.data.userData
  282. })
  283. }else{
  284. wx.showToast({
  285. title: option.msg,
  286. icon: 'none'
  287. })
  288. }
  289. }
  290. })
  291. }
  292. })
  293. },
  294. changeSelectType:function(e){
  295. if(e){
  296. this.setData({
  297. selectType:e.currentTarget.dataset.type
  298. })
  299. }else{
  300. this.setData({
  301. selectType:1
  302. })
  303. }
  304. },
  305. getCaptcha: function (e) {//获取用户输入的验证码
  306. this.data.submitData.captcha = e.detail.value;
  307. this.setData({
  308. submitData:this.data.submitData
  309. })
  310. },
  311. getCode: function (e) {//获取验证码
  312. if (!app.mobileVerify(this.data.mobile2)) {
  313. if (this.data.mobile2) {
  314. wx.showToast({
  315. title: '请输入正确的电话',
  316. icon: 'none'
  317. })
  318. } else {
  319. wx.showToast({
  320. title: '请输入电话',
  321. icon: 'none'
  322. })
  323. }
  324. return;
  325. }
  326. if (!this.data.sendCode) {
  327. return;
  328. }
  329. this.data.sendCode = false;
  330. // app.wxRequest(app.globalData.urlRoot + "captcha/sendCaptcha", { mobile: this.data.mobile2 }, res => {
  331. // if (res.code == 200) {
  332. this.countDown();
  333. wx.showToast({
  334. title: '验证码获取成功',
  335. icon: "none"
  336. })
  337. this.setData({
  338. verificationCode: 60
  339. })
  340. // } else {
  341. // this.data.sendCode = true;
  342. // wx.showToast({
  343. // title: res.msg,
  344. // icon: "none"
  345. // })
  346. // }
  347. // }, this)
  348. },
  349. countDown: function () {//倒计时
  350. setTimeout(() => {
  351. this.setData({
  352. verificationCode: this.data.verificationCode - 1
  353. })
  354. if (this.data.verificationCode > 0) {
  355. this.countDown();
  356. } else {
  357. this.setData({
  358. verificationCode: "获取验证码"
  359. })
  360. this.data.sendCode = true;
  361. }
  362. }, 1000);
  363. },
  364. getUserPhone: function (e) {//获取用户手机号
  365. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  366. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  367. if (res.code == 200) {
  368. this.setData({
  369. phoneInputShow: true
  370. })
  371. this.changeSelectType();
  372. if (res.data && res.data.decodeData) {
  373. this.data.submitData.mobile = res.data.decodeData.phoneNumber;
  374. this.setData({
  375. submitData: this.data.submitData
  376. })
  377. }
  378. } else {
  379. wx.showToast({
  380. title: res.msg,
  381. icon: "none"
  382. })
  383. }
  384. }, this);
  385. }
  386. },
  387. getUserLocation: function (e) {
  388. wx.getLocation({
  389. type: 'wgs84', //wgs84 gcj02
  390. success: (res) => {
  391. this.getDistributorList(res.longitude, res.latitude);
  392. },
  393. fail: (res) => {
  394. this.getDistributorList("", "");
  395. }
  396. })
  397. },
  398. getDistributorList: function (longitude, latitude) {//获取经销商列表
  399. app.wxRequest(app.globalData.urlRoot + "agent/getAgentList", { longitude: longitude, latitude: latitude }, res => {
  400. if (res.code == 200) {
  401. //整理数据
  402. var datas = res.data;
  403. var province = [];
  404. var city = [];
  405. for (let i = 0; i < res.data.list.length; i++) {
  406. province.push(res.data.list[i].province);
  407. }
  408. for(let i=0;i<province.length;i++){
  409. if(province[i]==this.data.submitData.province){
  410. res.data.nearData.provinceIndex = i;
  411. }
  412. }
  413. for (let j = 0; j < res.data.list[res.data.nearData.provinceIndex].children.length; j++) {
  414. city.push(res.data.list[res.data.nearData.provinceIndex].children[j].city)
  415. }
  416. for(let i=0;i<city.length;i++){
  417. if(city[i]==this.data.submitData.city){
  418. res.data.nearData.cityIndex = i;
  419. }
  420. }
  421. if(this.data.submitData.agentDetail){
  422. for(let i=0;i<res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children.length;i++){
  423. if(res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children[i]==this.data.submitData.agentDetail){
  424. res.data.nearData.agentIndex = i;
  425. }
  426. }
  427. }else{
  428. res.data.nearData.agentIndex = 0;
  429. }
  430. //将数据赋值给变量
  431. this.setData({
  432. provinceDataAll: res.data.list,
  433. provinceDataArr: [province, city],
  434. provinceDataValue: [res.data.nearData.provinceIndex, res.data.nearData.cityIndex],
  435. nowProvince: province[res.data.nearData.provinceIndex] + " " + city[res.data.nearData.cityIndex],
  436. storeArr: res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children,
  437. storeValue: res.data.nearData.agentIndex
  438. })
  439. } else {
  440. wx.showToast({
  441. title: res.msg,
  442. icon: "none"
  443. })
  444. }
  445. }, this);
  446. },
  447. provinceDataChange: function (e) {
  448. if (e.detail.column == 0) {
  449. var city = [];
  450. for (let i = 0; i < this.data.provinceDataAll[e.detail.value].children.length; i++) {
  451. city.push(this.data.provinceDataAll[e.detail.value].children[i].city);
  452. }
  453. this.data.provinceDataArr[1] = city;
  454. this.setData({
  455. provinceDataArr: this.data.provinceDataArr
  456. })
  457. }
  458. },
  459. provinceDataChane: function (e) {
  460. this.setData({
  461. provinceDataValue: e.detail.value,
  462. nowProvince: this.data.provinceDataArr[0][e.detail.value[0]] + " " + this.data.provinceDataArr[1][e.detail.value[1]],
  463. storeArr: this.data.provinceDataAll[e.detail.value[0]].children[e.detail.value[1]].children,
  464. storeValue: 0
  465. })
  466. },
  467. storeChane: function (e) {
  468. this.setData({
  469. storeValue: e.detail.value
  470. })
  471. }
  472. }
  473. })