ConfigController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Cache;
  10. use common\helpers\Form;
  11. use common\models\forms\ReceiveAddressForm;
  12. use common\models\forms\UserConfigForm;
  13. use common\models\ReceiveAddress;
  14. use common\models\Region;
  15. use common\models\UserInfo;
  16. use yii\base\Exception;
  17. class ConfigController extends BaseController
  18. {
  19. public $modelClass = UserInfo::class;
  20. /**
  21. * 会员的相关配置
  22. * @return mixed
  23. * @throws \yii\web\HttpException
  24. */
  25. public function actionBase(){
  26. /**/
  27. $data = UserInfo::find()->select('IS_AUTO_WITHDRAW,ALLOW_RECONSUME_SMS')->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->asArray()->one();
  28. foreach($data as $key=>$value){
  29. if($key == 'IS_AUTO_WITHDRAW'){
  30. $data[$key] = boolval($value);
  31. }
  32. if($key == 'ALLOW_RECONSUME_SMS'){
  33. $data[$key] = boolval($value);
  34. }
  35. }
  36. $data['smsFee'] = Cache::getSystemConfig()['smsFee']['VALUE'];
  37. return static::notice($data);
  38. }
  39. /**
  40. * 修改自动提现设置
  41. * @return mixed
  42. * @throws \yii\db\Exception
  43. * @throws \yii\web\HttpException
  44. */
  45. public function actionAutoWithdraw(){
  46. if(\Yii::$app->request->isPost){
  47. $formModel = new UserConfigForm();
  48. $formModel->scenario = 'autoWithdraw';
  49. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->autoWithdraw()){
  50. return static::notice('开启关闭自动提现成功');
  51. } else {
  52. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  53. }
  54. }
  55. }
  56. /**
  57. * 修改复销短信通知设置
  58. * @return mixed
  59. * @throws \yii\base\Exception
  60. * @throws \yii\db\Exception
  61. * @throws \yii\web\HttpException
  62. */
  63. public function actionAllowReconsumeSms(){
  64. if(\Yii::$app->request->isPost){
  65. $formModel = new UserConfigForm();
  66. $formModel->scenario = 'allowReconsumeSms';
  67. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->allowReconsumeSms()){
  68. return static::notice('开启关闭复销短信提醒成功');
  69. } else {
  70. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  71. }
  72. }
  73. }
  74. /**
  75. * 收货地址列表
  76. * @return mixed
  77. * @throws \yii\web\HttpException
  78. */
  79. public function actionReceiveAddressList() {
  80. $condition = ' AND USER_ID=:USER_ID';
  81. $params[':USER_ID'] = \Yii::$app->user->id;
  82. $data = ReceiveAddress::lists($condition, $params, [
  83. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,CITY,COUNTY,ADDRESS,IS_DEFAULT',
  84. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  85. 'useSlaves' => true,
  86. ]);
  87. if($data['list']){
  88. foreach($data['list'] as $key=>$row){
  89. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  90. $data['list'][$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  91. $data['list'][$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  92. }
  93. }
  94. return static::notice($data);
  95. }
  96. /**
  97. * 获取一条收货地址
  98. * @return mixed
  99. * @throws \yii\web\HttpException
  100. */
  101. public function actionReceiveAddressOne() {
  102. $data = ReceiveAddress::findOneAsArray('USER_ID=:USER_ID AND ID=:ID', [':USER_ID'=>\Yii::$app->user->id, ':ID'=>\Yii::$app->request->get('id')]);
  103. return static::notice($data);
  104. }
  105. /**
  106. * 添加收货地址
  107. * @return mixed
  108. * @throws \yii\web\HttpException
  109. */
  110. public function actionReceiveAddressAdd() {
  111. if(\Yii::$app->request->isPost) {
  112. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userAdd', ['edit']);
  113. }
  114. return static::notice('非法访问', 400);
  115. }
  116. /**
  117. * 编辑收货地址
  118. * @return mixed
  119. * @throws \yii\web\HttpException
  120. */
  121. public function actionReceiveAddressEdit() {
  122. if(\Yii::$app->request->isPost) {
  123. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userEdit', ['edit']);
  124. }
  125. return static::notice('非法访问', 400);
  126. }
  127. /**
  128. * 设置取消默认收货地址
  129. * @return mixed
  130. * @throws \yii\web\HttpException
  131. */
  132. public function actionReceiveAddressDefault() {
  133. if(\Yii::$app->request->isPost) {
  134. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userIsDefault', ['edit']);
  135. }
  136. return static::notice('非法访问', 400);
  137. }
  138. /**
  139. * 删除
  140. * @return mixed
  141. * @throws \yii\db\Exception
  142. * @throws \yii\web\HttpException
  143. */
  144. public function actionReceiveAddressDelete() {
  145. if(\Yii::$app->request->isPost) {
  146. return parent::delete(ReceiveAddress::class, null, function(){
  147. // 如果没有默认地址的话,就在设置一个默认地址
  148. if(!ReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID'=>\Yii::$app->user->id])->exists()){
  149. $model = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->one();
  150. $model->IS_DEFAULT = 1;
  151. if (!$model->save()){
  152. throw new Exception('更新默认地址失败');
  153. }
  154. }
  155. } , true);
  156. }
  157. return static::notice('非法访问', 400);
  158. }
  159. }