ConfigController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\helpers\snowflake\SnowFake;
  12. use common\models\BaReceiveAddress;
  13. use common\models\forms\BaReceiveAddressForm;
  14. use common\models\forms\ReceiveAddressForm;
  15. use common\models\forms\UserConfigForm;
  16. use common\models\ReceiveAddress;
  17. use common\models\Region;
  18. use common\models\UserInfo;
  19. use yii\base\Exception;
  20. class ConfigController extends BaseController
  21. {
  22. public $modelClass = UserInfo::class;
  23. /**
  24. * 会员的相关配置
  25. * @return mixed
  26. * @throws \yii\web\HttpException
  27. */
  28. public function actionBase(){
  29. /**/
  30. $data = UserInfo::find()->select('IS_AUTO_WITHDRAW,ALLOW_RECONSUME_SMS')->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->asArray()->one();
  31. foreach($data as $key=>$value){
  32. if($key == 'IS_AUTO_WITHDRAW'){
  33. $data[$key] = boolval($value);
  34. }
  35. if($key == 'ALLOW_RECONSUME_SMS'){
  36. $data[$key] = boolval($value);
  37. }
  38. }
  39. $data['smsFee'] = Cache::getSystemConfig()['smsFee']['VALUE'];
  40. return static::notice($data);
  41. }
  42. /**
  43. * 修改自动提现设置
  44. * @return mixed
  45. * @throws \yii\db\Exception
  46. * @throws \yii\web\HttpException
  47. */
  48. public function actionAutoWithdraw(){
  49. if(\Yii::$app->request->isPost){
  50. $formModel = new UserConfigForm();
  51. $formModel->scenario = 'autoWithdraw';
  52. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->autoWithdraw()){
  53. return static::notice('开启关闭自动提现成功');
  54. } else {
  55. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  56. }
  57. }
  58. }
  59. /**
  60. * 修改复销短信通知设置
  61. * @return mixed
  62. * @throws \yii\base\Exception
  63. * @throws \yii\db\Exception
  64. * @throws \yii\web\HttpException
  65. */
  66. public function actionAllowReconsumeSms(){
  67. if(\Yii::$app->request->isPost){
  68. $formModel = new UserConfigForm();
  69. $formModel->scenario = 'allowReconsumeSms';
  70. if($formModel->load(\Yii::$app->request->post(), '') && $formModel->allowReconsumeSms()){
  71. return static::notice('开启关闭复销短信提醒成功');
  72. } else {
  73. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  74. }
  75. }
  76. }
  77. /**
  78. * 收货地址列表
  79. * @return mixed
  80. * @throws \yii\web\HttpException
  81. */
  82. public function actionReceiveAddressList() {
  83. $condition = ' AND USER_ID=:USER_ID';
  84. $params[':USER_ID'] = \Yii::$app->user->id;
  85. $data = ReceiveAddress::lists($condition, $params, [
  86. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
  87. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  88. 'useSlaves' => true,
  89. ]);
  90. if($data['list']){
  91. foreach($data['list'] as $key=>$row){
  92. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  93. // $data['list'][$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  94. // $data['list'][$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  95. }
  96. }
  97. // print_r($data);exit;
  98. return static::notice($data);
  99. }
  100. /**
  101. * 获取一条收货地址
  102. * @return mixed
  103. * @throws \yii\web\HttpException
  104. */
  105. public function actionReceiveAddressOne() {
  106. $data = ReceiveAddress::findOneAsArray('USER_ID=:USER_ID AND ID=:ID', [':USER_ID'=>\Yii::$app->user->id, ':ID'=>\Yii::$app->request->get('id')]);
  107. return static::notice($data);
  108. }
  109. /**
  110. * 添加收货地址
  111. * @return mixed
  112. * @throws \yii\web\HttpException
  113. */
  114. public function actionReceiveAddressAdd() {
  115. Region::updateToCache();
  116. if(\Yii::$app->request->isPost) {
  117. return parent::edit(ReceiveAddressForm::class, 'Added Successfully', 'userAdd', ['edit']);
  118. }
  119. return static::notice('Illegal request', 400); // 非法请求
  120. }
  121. /**
  122. * 编辑收货地址
  123. * @return mixed
  124. * @throws \yii\web\HttpException
  125. */
  126. public function actionReceiveAddressEdit() {
  127. if(\Yii::$app->request->isPost) {
  128. return parent::edit(ReceiveAddressForm::class, 'Modified Successfully', 'userEdit', ['edit']);
  129. }
  130. return static::notice('Illegal request', 400); // 非法请求
  131. }
  132. /**
  133. * 设置取消默认收货地址
  134. * @return mixed
  135. * @throws \yii\web\HttpException
  136. */
  137. public function actionReceiveAddressDefault() {
  138. if(\Yii::$app->request->isPost) {
  139. return parent::edit(ReceiveAddressForm::class, '添加收货地址成功', 'userIsDefault', ['edit']);
  140. }
  141. return static::notice('Illegal request', 400); // 非法请求
  142. }
  143. /**
  144. * 删除
  145. * @return mixed
  146. * @throws \yii\db\Exception
  147. * @throws \yii\web\HttpException
  148. */
  149. public function actionReceiveAddressDelete() {
  150. if(\Yii::$app->request->isPost) {
  151. return parent::delete(ReceiveAddress::class, null, function(){
  152. // 如果没有默认地址的话,就在设置一个默认地址
  153. if(!ReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID'=>\Yii::$app->user->id])->exists()){
  154. $model = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->one();
  155. $model->IS_DEFAULT = 1;
  156. if (!$model->save()){
  157. throw new Exception('更新默认地址失败');
  158. }
  159. }
  160. } , true);
  161. }
  162. return static::notice('Illegal request', 400); // 非法请求
  163. }
  164. /**
  165. * 收货地址列表
  166. * @return mixed
  167. * @throws \yii\web\HttpException
  168. */
  169. public function actionBaReceiveAddressList() {
  170. $condition = ' AND USER_ID=:USER_ID';
  171. $params[':USER_ID'] = \Yii::$app->user->id;
  172. $data = BaReceiveAddress::lists($condition, $params, [
  173. 'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
  174. 'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
  175. 'useSlaves' => true,
  176. ]);
  177. if($data['list']){
  178. foreach($data['list'] as $key=>$row){
  179. $data['list'][$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  180. }
  181. }
  182. return static::notice($data);
  183. }
  184. /**
  185. * 获取一条收货地址
  186. * @return mixed
  187. * @throws \yii\web\HttpException
  188. */
  189. public function actionBaReceiveAddressOne() {
  190. $data = BaReceiveAddress::findOneAsArray(
  191. 'USER_ID=:USER_ID AND ID=:ID',
  192. [':USER_ID'=>\Yii::$app->user->id, ':ID' => \Yii::$app->request->get('id')]
  193. );
  194. return static::notice($data);
  195. }
  196. /**
  197. * 添加收货地址
  198. * @return mixed
  199. * @throws \yii\web\HttpException
  200. */
  201. public function actionBaReceiveAddressAdd() {
  202. Region::updateToCache();
  203. if (\Yii::$app->request->isPost) {
  204. return parent::edit(BaReceiveAddressForm::class, 'Added Successfully', 'userAdd', ['edit']);
  205. }
  206. return static::notice('Illegal request', 400); // 非法请求
  207. }
  208. /**
  209. * 编辑收货地址
  210. * @return mixed
  211. * @throws \yii\web\HttpException
  212. */
  213. public function actionBaReceiveAddressEdit() {
  214. if(\Yii::$app->request->isPost) {
  215. return parent::edit(BaReceiveAddressForm::class, 'Modified Successfully', 'userEdit', ['edit']);
  216. }
  217. return static::notice('Illegal request', 400); // 非法请求
  218. }
  219. /**
  220. * 设置取消默认收货地址
  221. * @return mixed
  222. * @throws \yii\web\HttpException
  223. */
  224. public function actionBaReceiveAddressDefault() {
  225. if(\Yii::$app->request->isPost) {
  226. return parent::edit(BaReceiveAddressForm::class, '添加收货地址成功', 'userIsDefault', ['edit']);
  227. }
  228. return static::notice('Illegal request', 400); // 非法请求
  229. }
  230. /**
  231. * 删除
  232. * @return mixed
  233. * @throws \yii\db\Exception
  234. * @throws \yii\web\HttpException
  235. */
  236. public function actionBaReceiveAddressDelete() {
  237. if(\Yii::$app->request->isPost) {
  238. return parent::delete(BaReceiveAddress::class, null, function() {
  239. // 如果没有默认地址的话,就在设置一个默认地址
  240. if (!BaReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID' => \Yii::$app->user->id])->exists()) {
  241. $addressNumber = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->count();
  242. if ($addressNumber >= 1) {
  243. $model = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->one();
  244. $model->IS_DEFAULT = 1;
  245. if (!$model->save()) {
  246. throw new Exception('更新默认地址失败');
  247. }
  248. }
  249. }
  250. } , true);
  251. }
  252. return static::notice('Illegal request', 400); // 非法请求
  253. }
  254. }