User.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Goods;
  4. use app\common\controller\Api;
  5. use app\common\library\Ems;
  6. use app\common\library\Sms;
  7. use app\common\library\Token;
  8. use app\common\model\Order;
  9. use app\common\model\OrderInvoice;
  10. use fast\Random;
  11. use think\Config;
  12. use think\Db;
  13. use think\Exception;
  14. use think\Hook;
  15. use think\Validate;
  16. /**
  17. * 会员接口
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. if (!Config::get('fastadmin.usercenter')) {
  27. $this->error(__('User center already closed'));
  28. }
  29. }
  30. /**
  31. * 会员中心
  32. */
  33. public function index()
  34. {
  35. $this->auth->getUser()->head_img = config('site.head_img');
  36. $this->auth->getUser()->gw_name = config('site.gw_name');
  37. $this->auth->getUser()->dingding = config('site.dingding');
  38. $this->auth->getUser()->qywx = config('site.qywx');
  39. $this->success('', ['welcome' => $this->auth->getUser()]);
  40. }
  41. /**
  42. * 会员登录
  43. *
  44. * @ApiMethod (POST)
  45. * @param string $account 账号
  46. * @param string $password 密码
  47. */
  48. public function login()
  49. {
  50. $account = $this->request->post('account');
  51. $password = $this->request->post('password');
  52. if (!$account || !$password) {
  53. $this->error(__('Invalid parameters'));
  54. }
  55. $ret = $this->auth->login($account, $password);
  56. if ($ret) {
  57. $data = ['userinfo' => $this->auth->getUserinfo()];
  58. $this->success(__('Logged in successful'), $data);
  59. } else {
  60. $this->error($this->auth->getError());
  61. }
  62. }
  63. /**
  64. * 手机验证码登录
  65. *
  66. * @ApiMethod (POST)
  67. * @param string $mobile 手机号
  68. * @param string $captcha 验证码
  69. */
  70. public function mobilelogin()
  71. {
  72. $mobile = $this->request->post('mobile');
  73. $captcha = $this->request->post('captcha');
  74. // if (!$mobile || !$captcha) {
  75. // $this->error(__('Invalid parameters'));
  76. // }
  77. if (!Validate::regex($mobile, "^1\d{10}$")) {
  78. $this->error(__('Mobile is incorrect'));
  79. }
  80. // if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  81. // $this->error(__('Captcha is incorrect'));
  82. // }
  83. $user = \app\common\model\User::getByMobile($mobile);
  84. if ($user) {
  85. if ($user->status != 'normal') {
  86. $this->error(__('Account is locked'));
  87. }
  88. //如果已经有账号则直接登录
  89. $ret = $this->auth->direct($user->id);
  90. } else {
  91. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  92. }
  93. if ($ret) {
  94. Sms::flush($mobile, 'mobilelogin');
  95. $data = ['userinfo' => $this->auth->getUserinfo()];
  96. $this->success(__('Logged in successful'), $data);
  97. } else {
  98. $this->error($this->auth->getError());
  99. }
  100. }
  101. /**
  102. * 注册会员
  103. *
  104. * @ApiMethod (POST)
  105. * @param string $username 用户名
  106. * @param string $password 密码
  107. * @param string $mobile 手机号
  108. * @param string $code 验证码
  109. */
  110. public function register()
  111. {
  112. $username = $this->request->post('username');
  113. $password = $this->request->post('password');
  114. // $email = $this->request->post('email');
  115. $mobile = $this->request->post('mobile');
  116. $code = $this->request->post('code');
  117. if (!$username) {
  118. $this->error(__('Invalid parameters'));
  119. }
  120. // if ($email && !Validate::is($email, "email")) {
  121. // $this->error(__('Email is incorrect'));
  122. // }
  123. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  124. $this->error(__('Mobile is incorrect'));
  125. }
  126. // $ret = Sms::check($mobile, $code, 'register');
  127. // if ($ret != 8888) {
  128. // $this->error(__('Captcha is incorrect'));
  129. // }
  130. $ret = $this->auth->register($username, $mobile, $password,[]);
  131. if ($ret) {
  132. $data = ['userinfo' => $this->auth->getUserinfo()];
  133. $this->success(__('Sign up successful'), $data);
  134. } else {
  135. $this->error($this->auth->getError());
  136. }
  137. }
  138. /**
  139. * 退出登录
  140. * @ApiMethod (POST)
  141. */
  142. public function logout()
  143. {
  144. if (!$this->request->isPost()) {
  145. $this->error(__('Invalid parameters'));
  146. }
  147. $this->auth->logout();
  148. $this->success(__('Logout successful'));
  149. }
  150. /**
  151. * 修改会员个人信息
  152. *
  153. * @ApiMethod (POST)
  154. * @param string $avatar 头像地址
  155. * @param string $username 用户名
  156. * @param string $nickname 昵称
  157. * @param string $bio 个人简介
  158. * @param string $company 公司
  159. * @param string $company_site 公司地址
  160. */
  161. public function profile()
  162. {
  163. $user = $this->auth->getUser();
  164. $username = $this->request->post('username');
  165. $nickname = $this->request->post('nickname');
  166. $bio = $this->request->post('bio');
  167. $company = $this->request->post('company');
  168. $company_site = $this->request->post('company_site');
  169. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  170. if($avatar){
  171. $user->avatar = $avatar;
  172. }
  173. if ($username) {
  174. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  175. if ($exists) {
  176. $this->error(__('Username already exists'));
  177. }
  178. $user->username = $username;
  179. }
  180. if ($nickname) {
  181. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  182. if ($exists) {
  183. $this->error(__('Nickname already exists'));
  184. }
  185. $user->nickname = $nickname;
  186. }
  187. $user->bio = $bio;
  188. $user->company = $company;
  189. $user->company_site = $company_site;
  190. $user->save();
  191. $this->success();
  192. }
  193. /**
  194. * 修改邮箱
  195. *
  196. * @ApiMethod (POST)
  197. * @param string $email 邮箱
  198. * @param string $captcha 验证码
  199. */
  200. public function changeemail()
  201. {
  202. $user = $this->auth->getUser();
  203. $email = $this->request->post('email');
  204. $captcha = $this->request->post('captcha');
  205. if (!$email || !$captcha) {
  206. $this->error(__('Invalid parameters'));
  207. }
  208. if (!Validate::is($email, "email")) {
  209. $this->error(__('Email is incorrect'));
  210. }
  211. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  212. $this->error(__('Email already exists'));
  213. }
  214. $result = Ems::check($email, $captcha, 'changeemail');
  215. if (!$result) {
  216. $this->error(__('Captcha is incorrect'));
  217. }
  218. $verification = $user->verification;
  219. $verification->email = 1;
  220. $user->verification = $verification;
  221. $user->email = $email;
  222. $user->save();
  223. Ems::flush($email, 'changeemail');
  224. $this->success();
  225. }
  226. /**
  227. * 修改手机号
  228. *
  229. * @ApiMethod (POST)
  230. * @param string $mobile 手机号
  231. * @param string $captcha 验证码
  232. */
  233. public function changemobile()
  234. {
  235. $user = $this->auth->getUser();
  236. $mobile = $this->request->post('mobile');
  237. $captcha = $this->request->post('captcha');
  238. if (!$mobile || !$captcha) {
  239. $this->error(__('Invalid parameters'));
  240. }
  241. if (!Validate::regex($mobile, "^1\d{10}$")) {
  242. $this->error(__('Mobile is incorrect'));
  243. }
  244. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  245. $this->error(__('Mobile already exists'));
  246. }
  247. $result = Sms::check($mobile, $captcha, 'changemobile');
  248. if (!$result) {
  249. $this->error(__('Captcha is incorrect'));
  250. }
  251. $verification = $user->verification;
  252. $verification->mobile = 1;
  253. $user->verification = $verification;
  254. $user->mobile = $mobile;
  255. $user->save();
  256. Sms::flush($mobile, 'changemobile');
  257. $this->success();
  258. }
  259. /**
  260. * 第三方登录
  261. *
  262. * @ApiMethod (POST)
  263. * @param string $platform 平台名称
  264. * @param string $code Code码
  265. */
  266. public function third()
  267. {
  268. $url = url('user/index');
  269. $platform = $this->request->post("platform");
  270. $code = $this->request->post("code");
  271. $config = get_addon_config('third');
  272. if (!$config || !isset($config[$platform])) {
  273. $this->error(__('Invalid parameters'));
  274. }
  275. $app = new \addons\third\library\Application($config);
  276. //通过code换access_token和绑定会员
  277. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  278. if ($result) {
  279. $loginret = \addons\third\library\Service::connect($platform, $result);
  280. if ($loginret) {
  281. $data = [
  282. 'userinfo' => $this->auth->getUserinfo(),
  283. 'thirdinfo' => $result
  284. ];
  285. $this->success(__('Logged in successful'), $data);
  286. }
  287. }
  288. $this->error(__('Operation failed'), $url);
  289. }
  290. /**
  291. * 重置密码
  292. *
  293. * @ApiMethod (POST)
  294. * @param string $mobile 手机号
  295. * @param string $newpassword 新密码
  296. * @param string $captcha 验证码
  297. */
  298. public function resetpwd()
  299. {
  300. $type = $this->request->post("type");
  301. $mobile = $this->request->post("mobile");
  302. $email = $this->request->post("email");
  303. $newpassword = $this->request->post("newpassword");
  304. $captcha = $this->request->post("captcha");
  305. if (!$newpassword || !$captcha) {
  306. $this->error(__('Invalid parameters'));
  307. }
  308. //验证Token
  309. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  310. $this->error(__('Password must be 6 to 30 characters'));
  311. }
  312. // if ($type == 'mobile') {
  313. if (!Validate::regex($mobile, "^1\d{10}$")) {
  314. $this->error(__('Mobile is incorrect'));
  315. }
  316. $user = \app\common\model\User::getByMobile($mobile);
  317. if (!$user) {
  318. $this->error(__('User not found'));
  319. }
  320. // $ret = Sms::check($mobile, $captcha, 'resetpwd');
  321. // if (!$ret) {
  322. // $this->error(__('Captcha is incorrect'));
  323. // }
  324. // Sms::flush($mobile, 'resetpwd');
  325. // } else {
  326. // if (!Validate::is($email, "email")) {
  327. // $this->error(__('Email is incorrect'));
  328. // }
  329. // $user = \app\common\model\User::getByEmail($email);
  330. // if (!$user) {
  331. // $this->error(__('User not found'));
  332. // }
  333. // $ret = Ems::check($email, $captcha, 'resetpwd');
  334. // if (!$ret) {
  335. // $this->error(__('Captcha is incorrect'));
  336. // }
  337. // Ems::flush($email, 'resetpwd');
  338. // }
  339. //模拟一次登录
  340. $this->auth->direct($user->id);
  341. $ret = $this->auth->changepwd($newpassword, '', true);
  342. if ($ret) {
  343. $this->success(__('Reset password successful'));
  344. } else {
  345. $this->error($this->auth->getError());
  346. }
  347. }
  348. /**
  349. * 修改密码
  350. *
  351. * @ApiMethod (POST)
  352. * @param string $formerpassword 手机号
  353. * @param string $newpassword 新密码
  354. * @param string $newpassword_verify 确认密码
  355. */
  356. public function change_pwd()
  357. {
  358. $type = $this->request->post("type");
  359. $mobile = $this->request->post("mobile");
  360. $email = $this->request->post("email");
  361. $formerpassword = $this->request->post("formerpassword");
  362. $newpassword = $this->request->post("newpassword");
  363. $newpassword_verify = $this->request->post("newpassword_verify");
  364. $captcha = $this->request->post("captcha");
  365. if (!$formerpassword || !$newpassword || !$newpassword_verify) {
  366. $this->error('参数缺失');
  367. }
  368. if($newpassword != $newpassword_verify){
  369. $this->error('新密码不一致');
  370. }
  371. //验证Token
  372. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  373. $this->error(__('Password must be 6 to 30 characters'));
  374. }
  375. // if ($type == 'mobile') {
  376. // if (!Validate::regex($mobile, "^1\d{10}$")) {
  377. // $this->error(__('Mobile is incorrect'));
  378. // }
  379. $user = \app\common\model\User::getByMobile($this->auth->mobile);
  380. if (!$user) {
  381. $this->error(__('User not found'));
  382. }
  383. if($this->auth->getEncryptPassword($formerpassword,$user->salt) != $user->password){
  384. $this->error('旧密码错误');
  385. }
  386. // $ret = Sms::check($mobile, $captcha, 'resetpwd');
  387. // if (!$ret) {
  388. // $this->error(__('Captcha is incorrect'));
  389. // }
  390. // Sms::flush($mobile, 'resetpwd');
  391. // } else {
  392. // if (!Validate::is($email, "email")) {
  393. // $this->error(__('Email is incorrect'));
  394. // }
  395. // $user = \app\common\model\User::getByEmail($email);
  396. // if (!$user) {
  397. // $this->error(__('User not found'));
  398. // }
  399. // $ret = Ems::check($email, $captcha, 'resetpwd');
  400. // if (!$ret) {
  401. // $this->error(__('Captcha is incorrect'));
  402. // }
  403. // Ems::flush($email, 'resetpwd');
  404. // }
  405. //模拟一次登录
  406. $this->auth->direct($user->id);
  407. $ret = $this->auth->changepwd($newpassword, '', true);
  408. if ($ret) {
  409. $this->success(__('Reset password successful'));
  410. } else {
  411. $this->error($this->auth->getError());
  412. }
  413. }
  414. /**
  415. * 注销用户
  416. */
  417. public function cancellation(){
  418. if (\app\common\model\User::update(['status'=>'Hidden'],['id'=>$this->auth->id])){
  419. $this->auth->logout();
  420. $this->success('注销成功');
  421. }
  422. $this->error('注销失败');
  423. }
  424. /**
  425. * 我的订单列表
  426. * @ApiMethod (POST)
  427. * @ApiReturnParams (name="uid",description="用户id")
  428. * @ApiReturnParams (name="type",description="订单类型 1订单 2报价单")
  429. * @ApiReturnParams (name="customization",description="定制金额")
  430. * @ApiReturnParams (name="goods_id",description="商品id")
  431. * @ApiReturnParams (name="goods_name",description="商品名称")
  432. * @ApiReturnParams (name="order_no",description="订单编号")
  433. * @ApiReturnParams (name="amount_real",description="订单金额")
  434. * @ApiReturnParams (name="status",description="订单流程状态(0已取消,1待支付,2已支付)")
  435. * @ApiReturnParams (name="create_time",description="创建时间")
  436. * @ApiReturnParams (name="deploy_type",description="部署方式0自己部署 1授权部署")
  437. * @ApiReturnParams (name="deploy",description="授权部署信息")
  438. * @ApiReturnParams (name="billing_status",description="开票状态 0未开票1已开票")
  439. * @ApiReturnParams (name="valid_time",description="报价单失效时间")
  440. */
  441. public function myorder(){
  442. $list = Order::all(function ($query){
  443. $query->where(['uid'=>$this->auth->id,'type'=>1])->field('id,uid,type,goods_id,goods_name,order_no,amount_real,status,create_time,deploy_type,deploy,billing_status')->order('id desc');
  444. });
  445. $bjlist = Order::all(function ($query){
  446. $query->where(['uid'=>$this->auth->id,'type'=>2,'status'=>2])->order('id desc');
  447. });
  448. $res = array_merge($list,$bjlist);
  449. $this->success('请求成功',$res);
  450. }
  451. /**
  452. * 我的订单详情
  453. * @ApiMethod (POST)
  454. * @ApiParams (name="id",description="订单id")
  455. * @ApiReturnParams (name="uid",description="用户id")
  456. * @ApiReturnParams (name="type",description="订单类型 1订单 2报价单")
  457. * @ApiReturnParams (name="customization",description="定制金额")
  458. * @ApiReturnParams (name="goods_id",description="商品id")
  459. * @ApiReturnParams (name="goods_name",description="商品名称")
  460. * @ApiReturnParams (name="order_no",description="订单编号")
  461. * @ApiReturnParams (name="amount_real",description="订单金额")
  462. * @ApiReturnParams (name="status",description="订单流程状态(0已取消,1待支付,2已支付)")
  463. * @ApiReturnParams (name="create_time",description="创建时间")
  464. * @ApiReturnParams (name="deploy_type",description="部署方式0自己部署 1授权部署")
  465. * @ApiReturnParams (name="deploy",description="授权部署信息")
  466. * @ApiReturnParams (name="billing_status",description="开票状态 0未开票1已开票")
  467. * @ApiReturnParams (name="valid_time",description="报价单失效时间")
  468. */
  469. public function myorder_details(){
  470. $list = Order::get(function ($query){
  471. $query->where(['uid'=>$this->auth->id,'type'=>1,'id'=>input('id')])->field('id,uid,type,goods_id,goods_name,order_no,amount_real,status,create_time,deploy_type,deploy,billing_status,after_sale,discount,discount_id')->order('id desc');
  472. });
  473. $this->success('请求成功',$list);
  474. }
  475. /**
  476. * 我的报价单列表
  477. * @ApiMethod (POST)
  478. * @ApiReturnParams (name="uid",description="用户id")
  479. * @ApiReturnParams (name="type",description="订单类型 1订单 2报价单")
  480. * @ApiReturnParams (name="customization",description="定制金额")
  481. * @ApiReturnParams (name="goods_id",description="商品id")
  482. * @ApiReturnParams (name="goods_name",description="商品名称")
  483. * @ApiReturnParams (name="order_no",description="订单编号")
  484. * @ApiReturnParams (name="amount_real",description="订单金额")
  485. * @ApiReturnParams (name="market",description="报价人")
  486. * @ApiReturnParams (name="status",description="订单流程状态(0已取消,1待支付,2已支付)")
  487. * @ApiReturnParams (name="create_time",description="创建时间")
  488. * @ApiReturnParams (name="deploy_type",description="部署方式0自己部署 1授权部署")
  489. * @ApiReturnParams (name="deploy",description="授权部署信息")
  490. * @ApiReturnParams (name="billing_status",description="开票状态 0未开票1已开票")
  491. * @ApiReturnParams (name="valid_time",description="报价单失效时间")
  492. */
  493. public function mypjorder(){
  494. $list = Order::all(function ($query){
  495. $query->where(['uid'=>$this->auth->id,'type'=>2])->where('status','<>',2)->field('id,uid,type,goods_id,goods_name,order_no,amount_real,market,status,create_time,deploy_type,deploy,billing_status,valid_time')->order('id desc');
  496. });
  497. // foreach ($list as $v){
  498. // $v['user'] = \app\admin\model\User::field('username,mobile,postcode,email,company,company_site')->find(['id'=>$v['uid']]);
  499. // }
  500. $this->success('请求成功',$list);
  501. }
  502. /**
  503. * 我的报价单详情
  504. * @ApiMethod (POST)
  505. * @ApiParams (name="id",description="订单id")
  506. * @ApiReturnParams (name="uid",description="用户id")
  507. * @ApiReturnParams (name="type",description="订单类型 1订单 2报价单")
  508. * @ApiReturnParams (name="customization",description="定制金额")
  509. * @ApiReturnParams (name="goods_id",description="商品id")
  510. * @ApiReturnParams (name="goods_name",description="商品名称")
  511. * @ApiReturnParams (name="order_no",description="订单编号")
  512. * @ApiReturnParams (name="amount_real",description="订单金额")
  513. * @ApiReturnParams (name="market",description="报价人")
  514. * @ApiReturnParams (name="status",description="订单流程状态(0已取消,1待支付,2已支付)")
  515. * @ApiReturnParams (name="create_time",description="创建时间")
  516. * @ApiReturnParams (name="deploy_type",description="部署方式0自己部署 1授权部署")
  517. * @ApiReturnParams (name="deploy",description="授权部署信息")
  518. * @ApiReturnParams (name="billing_status",description="开票状态 0未开票1已开票")
  519. * @ApiReturnParams (name="valid_time",description="报价单失效时间")
  520. */
  521. public function mypjorder_details(){
  522. $list = Order::get(function ($query){
  523. $query->where(['uid'=>$this->auth->id,'type'=>2,'id'=>input('id')])->field('id,uid,type,goods_id,goods_name,order_no,amount_real,market,status,create_time,deploy_type,deploy,billing_status,valid_time,customization')->order('id desc');
  524. });
  525. $list['price'] = Goods::where('id',$list['goods_id'])->value('price');
  526. $list['user'] = \app\admin\model\User::field('username,mobile,postcode,email,company,company_site')->find(['id'=>$list['uid']]);
  527. $this->success('请求成功',$list);
  528. }
  529. /**
  530. * 申请开票
  531. * @ApiMethod ("POST")
  532. * @ApiParams (name="cp_order_no",description="订单编号")
  533. * @ApiParams (name="invoice_type",description="发票类型 1普通发票2增值税发票")
  534. * @ApiParams (name="invoice_shape",description="发票形式 1纸质发票2电子发票")
  535. * @ApiParams (name="type",description="类型 1个人2企业")
  536. * @ApiParams (name="name",description="抬头名称")
  537. * @ApiParams (name="duty_paragraph",description="单位税号")
  538. * @ApiParams (name="address",description="注册地址")
  539. * @ApiParams (name="company_phone",description="公司电话")
  540. * @ApiParams (name="bank",description="银行")
  541. * @ApiParams (name="compellation",description="姓名")
  542. * @ApiParams (name="phone",description="个人电话")
  543. * @ApiParams (name="location",description="所在地")
  544. * @ApiParams (name="postcode",description="邮编")
  545. * @ApiParams (name="emial",description="邮箱")
  546. */
  547. public function make_ticket(){
  548. $uid = $this->auth->id;
  549. if(input('type')==1) {
  550. $rule = [
  551. 'cp_order_no'=>'require',
  552. 'invoice_type'=>'require',
  553. 'invoice_shape'=>'require',
  554. 'type'=>'require',
  555. 'name'=>'require',
  556. 'compellation'=>'require',
  557. 'phone'=>'require',
  558. 'location'=>'require',
  559. 'postcode'=>'require',
  560. 'emial'=>'require',
  561. ];
  562. }else if(input('type')==2){
  563. $rule = [
  564. 'cp_order_no'=>'require',
  565. 'invoice_type'=>'require',
  566. 'invoice_shape'=>'require',
  567. 'type'=>'require',
  568. 'name'=>'require',
  569. 'duty_paragraph'=>'require',
  570. 'address'=>'require',
  571. 'company_phone'=>'require',
  572. 'bank'=>'require',
  573. 'compellation'=>'require',
  574. 'phone'=>'require',
  575. 'location'=>'require',
  576. ];
  577. }
  578. $data = $this->_validate($rule);
  579. $data['uid'] = $uid;
  580. $data['order_no'] = pay_no($uid);
  581. if(!empty(Order::where('order_no', $data['cp_order_no'])->value('billing_status'))){
  582. $this->error('订单已申请发票');
  583. }
  584. DB::startTrans();
  585. try {
  586. $OrderInvoice = OrderInvoice::create($data);
  587. Order::where('order_no', $data['cp_order_no'])->update(['billing_status' => 1]);
  588. Db::commit();
  589. $this->success('申请成功',$OrderInvoice);
  590. }catch (Exception $exception){
  591. DB::rollback();
  592. $this->error($exception);
  593. return false;
  594. }
  595. }
  596. /**
  597. * 发票列表
  598. * @ApiMethod ("POST")
  599. * @ApiReturnParams (name="cp_order_no",description="产品订单编号")
  600. * @ApiReturnParams (name="order_no",description="订单编号")
  601. * @ApiReturnParams (name="invoice_type",description="发票类型 1普通发票2增值税发票")
  602. * @ApiReturnParams (name="invoice_shape",description="发票形式 1纸质发票2电子发票")
  603. * @ApiReturnParams (name="type",description="类型 1个人2企业")
  604. * @ApiReturnParams (name="name",description="抬头名称")
  605. * @ApiReturnParams (name="duty_paragraph",description="单位税号")
  606. * @ApiReturnParams (name="address",description="注册地址")
  607. * @ApiReturnParams (name="company_phone",description="公司电话")
  608. * @ApiReturnParams (name="bank",description="银行")
  609. * @ApiReturnParams (name="compellation",description="姓名")
  610. * @ApiReturnParams (name="phone",description="个人电话")
  611. * @ApiReturnParams (name="location",description="所在地")
  612. * @ApiReturnParams (name="postcode",description="邮编")
  613. * @ApiReturnParams (name="emial",description="邮箱")
  614. * @ApiReturnParams (name="apply_time",description="申请时间")
  615. * @ApiReturnParams (name="status",description="开票状态1待开票2已寄出3已完成")
  616. */
  617. public function invoice_list(){
  618. $data = OrderInvoice::all(function ($query){
  619. $query->where('uid',$this->auth->id)->order('id desc');
  620. });
  621. foreach ($data as $v){
  622. $v['order'] = Order::where('order_no',$v['cp_order_no'])->field('create_time,goods_name,amount_real,deploy_type,deploy')->find();
  623. }
  624. $this->success('请求成功',$data);
  625. }
  626. /**
  627. * 发票确认收货
  628. * @ApiParams (name="id")
  629. */
  630. public function invoice_receive(){
  631. $res = OrderInvoice::where('id',input('id'))->update(['status'=>3]);
  632. if ($res){
  633. $this->success('收货成功');
  634. }
  635. }
  636. }