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

249 line
6.9KB

  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. district: "",//地区
  20. addressDetail: ""//详细
  21. },
  22. avatarUrlShow:"",
  23. userData:{
  24. avatarUrl:"",
  25. nickName:""
  26. },
  27. isAddress: false,//是否有地址
  28. marginT: 0,
  29. },
  30. attached:function(){
  31. if (app.globalData.userPhoneType == "ios") {
  32. this.setData({
  33. marginT: -20
  34. })
  35. }
  36. if (app.globalData.openid) {
  37. this.loadFun();
  38. } else {
  39. app.globalData.openidSuccessFuc = this.loadFun;
  40. }
  41. },
  42. /**
  43. * 组件的方法列表
  44. */
  45. methods: {
  46. loadFun: function () {
  47. if (app.globalData.userInfoData) {
  48. this.data.userData.avatarUrl = app.globalData.userInfoData.avatarUrl;
  49. this.data.userData.nickName = app.globalData.userInfoData.nickName;
  50. this.setData({
  51. userData: this.data.userData,
  52. avatarUrlShow: app.globalData.userInfoData.avatarUrl
  53. })
  54. }
  55. this.getAddress();
  56. },
  57. getAddress: function () {//获取地址
  58. app.wxRequest(app.globalData.urlRoot + "address/getAddress", {}, res => {
  59. if (res.code == 200) {
  60. if (res.data) {
  61. this.data.isAddress = true;
  62. this.data.submitData.realName = res.data.realName;
  63. this.data.submitData.mobile = res.data.mobile;
  64. this.data.submitData.province = res.data.province;
  65. this.data.submitData.city = res.data.city;
  66. this.data.submitData.district = res.data.district;
  67. this.data.submitData.addressDetail = res.data.addressDetail;
  68. this.setData({
  69. submitData: this.data.submitData
  70. })
  71. }
  72. } else {
  73. wx.showToast({
  74. title: res.msg,
  75. icon: "none"
  76. })
  77. }
  78. }, this);
  79. },
  80. addAddress: function () {//添加地址
  81. app.wxRequest(app.globalData.urlRoot + "address/addAddress", this.data.submitData, res => {
  82. wx.showToast({
  83. title: res.msg,
  84. })
  85. if (res.code == 200) {
  86. if (app.globalData.skipType == 'mycenter') {
  87. app.globalData.skipType = null;
  88. wx.reLaunch({
  89. url: '/pages/myCenter/myCenter',
  90. })
  91. } else {
  92. this.closeXieyi();
  93. }
  94. }
  95. }, this, "POST")
  96. },
  97. updateAddress: function () {//更新地址
  98. app.wxRequest(app.globalData.urlRoot + "address/updateAddress", this.data.submitData, res => {
  99. wx.showToast({
  100. title: res.msg,
  101. })
  102. if (res.code == 200) {
  103. if (app.globalData.skipType == 'mycenter') {
  104. app.globalData.skipType = null;
  105. wx.reLaunch({
  106. url: '/pages/myCenter/myCenter',
  107. })
  108. } else {
  109. this.closeXieyi();
  110. }
  111. }
  112. }, this, "POST");
  113. },
  114. addressChange: function (e) {//所在地区发生改变
  115. this.data.submitData.province = e.detail.value[0];
  116. this.data.submitData.city = e.detail.value[1];
  117. this.data.submitData.district = e.detail.value[2];
  118. this.setData({
  119. submitData: this.data.submitData
  120. })
  121. },
  122. getNickName: function(e) {//获取昵称
  123. this.data.userData.nickName = e.detail.value;
  124. this.setData({
  125. userData: this.data.userData
  126. })
  127. },
  128. getRealName: function (e) {//获取收货人
  129. this.data.submitData.realName = e.detail.value;
  130. this.setData({
  131. submitData: this.data.submitData
  132. })
  133. },
  134. getMobile: function (e) {//获取手机号码
  135. this.data.submitData.mobile = e.detail.value;
  136. this.setData({
  137. submitData: this.data.submitData
  138. })
  139. },
  140. getAddressDetail: function (e) {//获取详细地址
  141. this.data.submitData.addressDetail = e.detail.value;
  142. this.setData({
  143. submitData: this.data.submitData
  144. })
  145. },
  146. saveUserMsg:function(){
  147. if (!this.data.submitData.realName) {
  148. wx.showToast({
  149. title: '请输入收货人',
  150. icon: "none"
  151. })
  152. return;
  153. }
  154. if (!app.mobileVerify(this.data.submitData.mobile)) {
  155. if (this.data.submitData.mobile) {
  156. wx.showToast({
  157. title: '请输入正确的电话',
  158. icon: 'none'
  159. })
  160. } else {
  161. wx.showToast({
  162. title: '请输入电话',
  163. icon: 'none'
  164. })
  165. }
  166. return;
  167. }
  168. if (!this.data.submitData.province) {
  169. wx.showToast({
  170. title: '请选择所在地区',
  171. icon: 'none'
  172. })
  173. return;
  174. }
  175. if (!this.data.submitData.addressDetail) {
  176. wx.showToast({
  177. title: '请输入详细地址',
  178. icon: "none"
  179. })
  180. return;
  181. }
  182. wx.showLoading({
  183. title: '保存中',
  184. mask: true
  185. })
  186. app.wxRequest(app.globalData.urlRoot + "userInfo/updateUserInfo", this.data.userData, res => {
  187. wx.hideLoading();
  188. if (res.code == 200) {
  189. app.globalData.userInfoData.avatarUrl = this.data.userData.avatarUrl;
  190. app.globalData.userInfoData.nickName = this.data.userData.nickName;
  191. var userInfoData = wx.getStorageSync("userInfoData");
  192. userInfoData.avatarUrl = this.data.userData.avatarUrl;
  193. userInfoData.nickName = this.data.userData.nickName;
  194. wx.setStorageSync('userInfoData', userInfoData)
  195. this.submitAddress();
  196. }
  197. }, this, "POST")
  198. },
  199. submitAddress: function () {//保存地址
  200. if (this.data.isAddress) {
  201. this.updateAddress();
  202. } else {
  203. this.addAddress();
  204. }
  205. },
  206. closeXieyi: function () {
  207. this.triggerEvent('address')
  208. },
  209. changeHeadImg:function(){
  210. wx.chooseImage({
  211. count:1,
  212. success:(res)=>{
  213. this.setData({
  214. avatarUrlShow: res.tempFilePaths[0]
  215. })
  216. wx.uploadFile({
  217. url: app.globalData.urlRoot + "upload/uploadImage",
  218. header: {
  219. "OPENID": app.globalData.openid,
  220. "VERSION": 100
  221. },
  222. filePath: res.tempFilePaths[0],
  223. name: "file",
  224. formData:{
  225. filePath:"avatar"
  226. },
  227. success: option => {
  228. var json = JSON.parse(option.data);
  229. if (json.code==200){
  230. this.data.userData.avatarUrl = json.data[0].url;
  231. this.setData({
  232. userData: this.data.userData
  233. })
  234. }else{
  235. wx.showToast({
  236. title: option.msg,
  237. icon: 'none'
  238. })
  239. }
  240. }
  241. })
  242. }
  243. })
  244. }
  245. }
  246. })