ConfigController.php 10 KB

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