Account.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Date;
  4. use ba\Captcha;
  5. use ba\Random;
  6. use think\console\output\Formatter;
  7. use think\facade\Db;
  8. use app\common\model\User;
  9. use app\common\facade\Token;
  10. use app\common\model\UserScoreLog;
  11. use app\common\model\UserMoneyLog;
  12. use app\common\controller\Frontend;
  13. use think\db\exception\PDOException;
  14. use think\exception\ValidateException;
  15. use app\api\validate\Account as AccountValidate;
  16. use think\facade\Validate;
  17. use app\common\model\FlightPlan as PlanModel;
  18. class Account extends Frontend
  19. {
  20. protected $noNeedLogin = ['retrievePassword'];
  21. protected $model = null;
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. }
  26. public function overview()
  27. {
  28. $sevenDays = Date::unixtime('day', -6);
  29. $score = $money = $days = [];
  30. for ($i = 0; $i < 7; $i++) {
  31. $days[$i] = date("Y-m-d", $sevenDays + ($i * 86400));
  32. $tempToday0 = strtotime($days[$i]);
  33. $temptoday24 = strtotime('+1 day', $tempToday0) - 1;
  34. $score[$i] = UserScoreLog::where('user_id', $this->auth->id)
  35. ->where('createtime', 'BETWEEN', $tempToday0 . ',' . $temptoday24)
  36. ->sum('score');
  37. $userMoneyTemp = UserMoneyLog::where('user_id', $this->auth->id)
  38. ->where('createtime', 'BETWEEN', $tempToday0 . ',' . $temptoday24)
  39. ->sum('money');
  40. $money[$i] = bcdiv($userMoneyTemp, 100, 2);
  41. }
  42. $this->success('', [
  43. 'days' => $days,
  44. 'score' => $score,
  45. 'money' => $money,
  46. ]);
  47. }
  48. public function profile()
  49. {
  50. if ($this->request->isPost()) {
  51. $data = $this->request->only(['id', 'avatar', 'username', 'nickname', 'gender', 'birthday', 'motto']);
  52. if (!isset($data['birthday'])) $data['birthday'] = null;
  53. Db::startTrans();
  54. try {
  55. $validate = new AccountValidate();
  56. $validate->scene('edit')->check($data);
  57. $this->auth->getUser()->where('id', $this->auth->id)->update($data);
  58. Db::commit();
  59. } catch (ValidateException|PDOException $e) {
  60. Db::rollback();
  61. $this->error($e->getMessage());
  62. }
  63. $this->success(__('Data updated successfully~'));
  64. }
  65. $this->success('', [
  66. 'accountVerificationType' => get_account_verification_type()
  67. ]);
  68. }
  69. public function realname()
  70. {
  71. if ($this->request->isPost()) {
  72. $data = $this->request->only(['real_name', 'identity', 'identity_img']);
  73. Db::startTrans();
  74. try {
  75. $validate = new AccountValidate();
  76. $validate->scene('realname')->check($data); // 验证规则,
  77. $this->auth->getUser()->where('id', $this->auth->id)->update($data);
  78. Db::commit();
  79. } catch (ValidateException|PDOException $e) {
  80. Db::rollback();
  81. $this->error($e->getMessage());
  82. }
  83. $this->success(__('Submitted successfully~'));
  84. }
  85. }
  86. public function certification()
  87. {
  88. if ($this->request->isPost()) {
  89. $data = $this->request->only(['license_type', 'pilot_license', 'city', 'address']);
  90. Db::startTrans();
  91. try {
  92. $validate = new AccountValidate();
  93. $validate->scene('certification')->check($data); // 验证规则,
  94. $this->auth->getUser()->where('id', $this->auth->id)->update($data);
  95. Db::commit();
  96. } catch (ValidateException|PDOException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. $this->success(__('Submitted successfully~'));
  101. }
  102. }
  103. public function applyFor()
  104. {
  105. if ($this->request->isPost()) {
  106. $data = $this->request->only(['username', 'real_name', 'license_type', 'mobile', 'pilot_license', 'city', 'flight_type', 'departure_point', 'planned_range', 'planned_height', 'aircraft_sn', 'aircraft_img', 'aircraft_brand', 'staff', 'staff_license', 'start_planning', 'end_planning']);
  107. Db::startTrans();
  108. try {
  109. $validate = new AccountValidate();
  110. $validate->scene('applyFor')->check($data); // 验证规则,
  111. // 更新用户表信息
  112. $this->auth->getUser()->where('id', $this->auth->id)->update($data);
  113. // 开始插入数据到新表
  114. $planModel = new PlanModel();
  115. $applyOrder = createOrderSn();
  116. $dataArr = [
  117. 'user_id' => $this->auth->id,
  118. 'username' => $this->auth->username,
  119. 'group_id' => $this->auth->group_id,
  120. 'applyfor_no' => $applyOrder,
  121. 'business_id' => $this->auth->business_id,
  122. 'real_name' => $data['real_name'],
  123. 'real_name_status' => $this->auth->real_name_status,
  124. 'license_type' => $data['license_type'],
  125. 'pilot_license' => $data['pilot_license'],
  126. 'type' => $data['flight_type'],
  127. 'city' => $data['city'],
  128. 'departure_point' => $data['departure_point'],
  129. 'range' => $data['planned_range'],
  130. 'height' => $data['planned_height'],
  131. 'aerocraft_sn' => $data['aircraft_sn'],
  132. 'aerocraft_img' => $data['aircraft_img'],
  133. 'start_time' => $data['start_planning'],
  134. 'end_time' => $data['end_planning'],
  135. 'staff' => $data['staff'],
  136. 'staff_license' => $data['staff_license'],
  137. 'createtime' => time(),
  138. ];
  139. $existingPlan = $planModel->where('user_id', $this->auth->id)
  140. ->whereDay('createtime')
  141. ->find();
  142. // Todo: unset 'applyfor_no' when update
  143. if ($existingPlan) {
  144. $planModel
  145. ->where('id', $existingPlan->id)
  146. ->data(['updatetime' => time()])
  147. ->update($dataArr);
  148. } else {
  149. $planModel->save($dataArr);
  150. }
  151. Db::commit();
  152. } catch (ValidateException|PDOException $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. $this->success(__('Submitted successfully~'));
  157. }
  158. }
  159. /**
  160. * 通过手机号或邮箱验证账户
  161. * 此处检查的验证码是通过 api/Ems或api/Sms发送的
  162. * 验证成功后,向前端返回一个 email-pass Token或着 mobile-pass Token
  163. * 在 changBind 方法中,通过 pass Token来确定用户已经通过了账户验证(用户未绑定邮箱/手机时通过账户密码验证)
  164. */
  165. public function verification()
  166. {
  167. $captcha = new Captcha();
  168. $params = $this->request->only(['type', 'captcha']);
  169. if ($captcha->check($params['captcha'], ($params['type'] == 'email' ? $this->auth->email : $this->auth->mobile) . "user_{$params['type']}_verify")) {
  170. $uuid = Random::uuid();
  171. Token::set($uuid, $params['type'] . '-pass', $this->auth->id, 600);
  172. $this->success('', [
  173. 'type' => $params['type'],
  174. 'accountVerificationToken' => $uuid,
  175. ]);
  176. }
  177. $this->error(__('Please enter the correct verification code'));
  178. }
  179. /**
  180. * 修改绑定信息(手机号、邮箱)
  181. * 通过 pass Token来确定用户已经通过了账户验证,也就是以上的 verification 方法,同时用户未绑定邮箱/手机时通过账户密码验证
  182. */
  183. public function changeBind()
  184. {
  185. $captcha = new Captcha();
  186. $params = $this->request->only(['type', 'captcha', 'email', 'mobile', 'accountVerificationToken', 'password']);
  187. $user = $this->auth->getUser();
  188. if ($user[$params['type']]) {
  189. if (!Token::check($params['accountVerificationToken'], $params['type'] . '-pass', $user->id, false)) {
  190. $this->error(__('You need to verify your account before modifying the binding information'));
  191. }
  192. } else {
  193. // 验证账户密码
  194. if (!isset($params['password']) || $user->password != encrypt_password($params['password'], $user->salt)) {
  195. $this->error(__('Password error'));
  196. }
  197. }
  198. // 检查验证码
  199. if ($captcha->check($params['captcha'], $params[$params['type']] . "user_change_{$params['type']}")) {
  200. if ($params['type'] == 'email') {
  201. $validate = Validate::rule(['email' => 'require|email|unique:user'])->message([
  202. 'email.require' => 'email format error',
  203. 'email.email' => 'email format error',
  204. 'email.unique' => 'email is occupied',
  205. ]);
  206. if (!$validate->check(['email' => $params['email']])) {
  207. $this->error(__($validate->getError()));
  208. }
  209. $user->email = $params['email'];
  210. } else if ($params['type'] == 'mobile') {
  211. $validate = Validate::rule(['mobile' => 'require|mobile|unique:user'])->message([
  212. 'mobile.require' => 'mobile format error',
  213. 'mobile.mobile' => 'mobile format error',
  214. 'mobile.unique' => 'mobile is occupied',
  215. ]);
  216. if (!$validate->check(['mobile' => $params['mobile']])) {
  217. $this->error(__($validate->getError()));
  218. }
  219. $user->mobile = $params['mobile'];
  220. }
  221. Token::delete($params['accountVerificationToken']);
  222. $user->save();
  223. $this->success();
  224. }
  225. $this->error(__('Please enter the correct verification code'));
  226. }
  227. public function changePassword()
  228. {
  229. if ($this->request->isPost()) {
  230. $params = $this->request->only(['oldPassword', 'newPassword']);
  231. if (!$this->auth->checkPassword($params['oldPassword'])) {
  232. $this->error(__('Old password error'));
  233. }
  234. Db::startTrans();
  235. try {
  236. $validate = new AccountValidate();
  237. $validate->scene('changePassword')->check(['password' => $params['newPassword']]);
  238. $this->auth->getUser()->resetPassword($this->auth->id, $params['newPassword']);
  239. Db::commit();
  240. } catch (ValidateException|PDOException $e) {
  241. Db::rollback();
  242. $this->error($e->getMessage());
  243. }
  244. $this->auth->logout();
  245. $this->success(__('Password has been changed, please login again~'));
  246. }
  247. }
  248. public function integral()
  249. {
  250. $limit = $this->request->request('limit');
  251. $integralModel = new UserScoreLog();
  252. $res = $integralModel->where('user_id', $this->auth->id)
  253. ->order('createtime desc')
  254. ->paginate($limit);
  255. $this->success('', [
  256. 'list' => $res->items(),
  257. 'total' => $res->total(),
  258. ]);
  259. }
  260. public function balance()
  261. {
  262. $limit = $this->request->request('limit');
  263. $moneyModel = new UserMoneyLog();
  264. $res = $moneyModel->where('user_id', $this->auth->id)
  265. ->order('createtime desc')
  266. ->paginate($limit);
  267. $this->success('', [
  268. 'list' => $res->items(),
  269. 'total' => $res->total(),
  270. ]);
  271. }
  272. public function retrievePassword()
  273. {
  274. $params = $this->request->only(['type', 'account', 'captcha', 'password']);
  275. try {
  276. $validate = new AccountValidate();
  277. $validate->scene('retrievePassword')->check($params);
  278. } catch (ValidateException $e) {
  279. $this->error($e->getMessage());
  280. }
  281. if ($params['type'] == 'email') {
  282. $user = User::where('email', $params['account'])->find();
  283. } else {
  284. $user = User::where('mobile', $params['account'])->find();
  285. }
  286. if (!$user) {
  287. $this->error(__('Account does not exist~'));
  288. }
  289. $captchaObj = new Captcha();
  290. if (!$captchaObj->check($params['captcha'], $params['account'] . 'user_retrieve_pwd')) {
  291. $this->error(__('Please enter the correct verification code'));
  292. }
  293. if ($user->resetPassword($user->id, $params['password'])) {
  294. $this->success(__('Password has been changed~'));
  295. } else {
  296. $this->error(__('Failed to modify password, please try again later~'));
  297. }
  298. }
  299. // 获取group_id,传给前端,
  300. public function getGroupId()
  301. {
  302. $this->success('', [
  303. 'group_id' => $this->auth->group_id,
  304. ]);
  305. }
  306. }