personal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <view>
  3. <view class="page-bottom-fixed">
  4. <!-- 主体内容 -->
  5. <block v-if="data_list_loding_status == 3">
  6. <form @submit="form_submit" class="form-container">
  7. <view class="padding-main oh">
  8. <view class="form-gorup oh">
  9. <view class="fw-b fl margin-top-lg">头像</view>
  10. <button class="fr bg-white br-0 lh-0" hover-class="none" open-type="chooseAvatar" @chooseavatar="choose_avatar_event" @tap="choose_avatar_event">
  11. <image :src="(user_data.avatar || default_avatar)" mode="widthFix" class="circle br user-avatar"></image>
  12. </button>
  13. </view>
  14. <view class="form-gorup oh">
  15. <view class="form-gorup-item-left">
  16. <view class="form-gorup-title">昵称<text class="form-group-tips-must">*</text></view>
  17. <input :type="(application_client_type == 'weixin') ? 'nickname' : 'text'" name="nickname" :value="user_data.nickname || ''" maxlength="16" placeholder-class="cr-grey" class="cr-base" placeholder="昵称2~16个字符">
  18. </view>
  19. <view class="form-gorup-item-right">
  20. <view class="form-gorup-title">生日</view>
  21. <picker name="birthday" mode="date" :value="user_data.birthday || ''" data-field="birthday" @change="select_change_event">
  22. <view :class="'picker '+((user_data.birthday || null) == null ? 'cr-grey' : '')">{{user_data.birthday || '请选择生日'}}</view>
  23. </picker>
  24. </view>
  25. </view>
  26. <view class="form-gorup">
  27. <view class="oh">
  28. <view class="form-gorup-item-left">
  29. <view class="form-gorup-title">所在省</view>
  30. <input type="text" name="province" :value="user_data.province || ''" maxlength="30" placeholder-class="cr-grey" class="cr-base" placeholder="所在省最多30个字符">
  31. </view>
  32. <view class="form-gorup-item-right">
  33. <view class="form-gorup-title">所在市</view>
  34. <input type="text" name="city" :value="user_data.city || ''" maxlength="30" placeholder-class="cr-grey" class="cr-base" placeholder="所在市最多30个字符">
  35. </view>
  36. </view>
  37. <view class="oh br-t">
  38. <view class="form-gorup-item-left padding-top-lg">
  39. <view class="form-gorup-title">所在区/县</view>
  40. <input type="text" name="county" :value="user_data.county || ''" maxlength="30" placeholder-class="cr-grey" class="cr-base" placeholder="所在区/县最多30个字符">
  41. </view>
  42. <view class="form-gorup-item-right padding-top-lg">
  43. <view class="form-gorup-title">详细地址</view>
  44. <input type="text" name="address" :value="user_data.address || ''" maxlength="30" placeholder-class="cr-grey" class="cr-base" placeholder="详细地址最多30个字符">
  45. </view>
  46. </view>
  47. </view>
  48. <view class="form-gorup oh">
  49. <view class="form-gorup-title">性别</view>
  50. <view class="radio-gorup">
  51. <radio-group data-field="gender" @change="select_change_event">
  52. <block v-for="(item,index) in gender_list" :key="index">
  53. <label class="radio">
  54. <radio :value="item.id.toString()" :checked="(parseInt(user_data.gender || 0) == item.id)" />
  55. <text class="va-m">{{item.name}}</text>
  56. </label>
  57. </block>
  58. </radio-group>
  59. </view>
  60. </view>
  61. <view class="bottom-fixed padding-main">
  62. <button class="bg-main br-main cr-white round text-size" type="default" form-type="submit" hover-class="none" :disabled="form_submit_disabled_status">保存</button>
  63. </view>
  64. </view>
  65. </form>
  66. </block>
  67. <!-- 错误提示 -->
  68. <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. const app = getApp();
  74. import componentNoData from "../../components/no-data/no-data";
  75. export default {
  76. data() {
  77. return {
  78. application_client_type: app.globalData.application_client_type(),
  79. data_list_loding_status: 1,
  80. data_list_loding_msg: '',
  81. form_submit_disabled_status: false,
  82. default_avatar: app.globalData.data.default_user_head_src,
  83. user_data: {},
  84. gender_list: []
  85. }
  86. },
  87. components: {
  88. componentNoData
  89. },
  90. onShow() {
  91. // 数据加载
  92. this.init();
  93. },
  94. methods: {
  95. // 获取数据
  96. init() {
  97. var user = app.globalData.get_user_info(this, 'init');
  98. if (user != false) {
  99. // 用户未绑定用户则转到登录页面
  100. if (app.globalData.user_is_need_login(user)) {
  101. uni.redirectTo({
  102. url: "/pages/login/login?event_callback=init"
  103. });
  104. this.setData({
  105. data_list_loding_status: 0,
  106. data_list_loding_msg: '请先绑定手机'
  107. });
  108. return false;
  109. } else {
  110. this.get_data();
  111. }
  112. } else {
  113. this.setData({
  114. data_list_loding_status: 0,
  115. data_list_loding_msg: '请先登录'
  116. });
  117. }
  118. },
  119. // 获取数据
  120. get_data() {
  121. uni.request({
  122. url: app.globalData.get_request_url("index", "personal"),
  123. method: 'POST',
  124. data: {},
  125. dataType: 'json',
  126. success: res => {
  127. if(res.data.code == 0) {
  128. this.setData({
  129. data_list_loding_status: 3,
  130. user_data: res.data.data.data || {},
  131. gender_list: res.data.data.gender_list || []
  132. });
  133. } else {
  134. this.setData({
  135. data_list_loding_status: 0,
  136. data_list_loding_msg: res.data.msg
  137. });
  138. }
  139. },
  140. fail: () => {
  141. this.setData({
  142. data_list_loding_status: 2,
  143. data_list_loding_msg: '服务器请求出错'
  144. });
  145. app.globalData.showToast('服务器请求出错');
  146. }
  147. });
  148. },
  149. // url事件
  150. url_event(e) {
  151. app.globalData.url_event(e);
  152. },
  153. // 生日、性别选择事件
  154. select_change_event(e) {
  155. var temp = this.user_data;
  156. temp[e.currentTarget.dataset.field] = e.detail.value;
  157. this.setData({user_data: temp});
  158. },
  159. // 头像事件
  160. choose_avatar_event(e) {
  161. var self = this;
  162. if(this.application_client_type == 'weixin') {
  163. self.upload_handle(e.detail.avatarUrl);
  164. } else {
  165. uni.chooseImage({
  166. count: 1,
  167. success(res) {
  168. if(res.tempFilePaths.length > 0) {
  169. self.upload_handle(res.tempFilePaths[0]);
  170. }
  171. }
  172. });
  173. }
  174. },
  175. // 上传处理
  176. upload_handle(image) {
  177. var self = this;
  178. uni.uploadFile({
  179. url: app.globalData.get_request_url('useravatarupload', 'personal'),
  180. filePath: image,
  181. name: 'file',
  182. formData: {},
  183. success: function(res) {
  184. if(res.statusCode == 200) {
  185. var data = typeof res.data == 'object' ? res.data : JSON.parse(res.data);
  186. if(data.code == 0) {
  187. var temp = self.user_data;
  188. temp['avatar'] = data.data;
  189. self.setData({user_data: temp});
  190. } else {
  191. app.globalData.showToast(data.msg);
  192. }
  193. }
  194. }
  195. });
  196. },
  197. // 数据提交
  198. form_submit(e) {
  199. // 表单数据
  200. var form_data = e.detail.value;
  201. // 头像
  202. form_data['avatar'] = this.user_data.avatar || '';
  203. // 生日
  204. form_data['birthday'] = this.user_data.birthday || '';
  205. // 性别
  206. form_data['gender'] = this.user_data.gender || 0;
  207. // 数据保存
  208. this.setData({
  209. form_submit_disabled_status: true
  210. });
  211. uni.showLoading({
  212. title: '处理中...'
  213. });
  214. uni.request({
  215. url: app.globalData.get_request_url('save', 'personal'),
  216. method: 'POST',
  217. data: form_data,
  218. dataType: 'json',
  219. success: res => {
  220. uni.hideLoading();
  221. this.setData({
  222. form_submit_disabled_status: false
  223. });
  224. if(res.data.code == 0) {
  225. uni.setStorageSync(app.globalData.data.cache_user_info_key, res.data.data);
  226. app.globalData.showToast(res.data.msg, 'success');
  227. setTimeout(function() {
  228. uni.navigateBack();
  229. }, 1000);
  230. } else {
  231. if (app.globalData.is_login_check(res.data)) {
  232. app.globalData.showToast(res.data.msg);
  233. } else {
  234. app.globalData.showToast('提交失败,请重试!');
  235. }
  236. }
  237. },
  238. fail: () => {
  239. uni.hideLoading();
  240. this.setData({
  241. form_submit_disabled_status: false
  242. });
  243. app.globalData.showToast('服务器请求出错');
  244. }
  245. });
  246. }
  247. }
  248. }
  249. </script>
  250. <style>
  251. @import './personal.css';
  252. </style>